package com.zhixi.pattern.important.responsibilitychainpattern.operation;

/**
 * @作者 ShuHaoNan
 * @邮箱 shuhaonan1@gmail.com
 * @时间 2020/11/17 14:08
 */
public abstract class Handler {

    /* 父亲 */
    public final static int FATHER_LEVEL_REQUEST = 1;
    /* 母亲 */
    public final static int MOTHER_LEVEL_REQUEST = 2;
    /* 哥哥 */
    public final static int BROTHER_LEVEL_REQUEST = 3;

    // 处理级别
    private int level = 0;
    // 下一个责任人
    private Handler nextHandler;

    public Handler(int level) {
        this.level = level;
    }

    /**
     * 责任链
     */
    public final void HandleMessage(Daughter daughter) {
        /* 如果是当前请求就处理 */
        if (daughter.getType() == this.level) {
            this.response(daughter);
        } else {
            /* 如果下一个责任人不为空，则递归调用 */
            if (this.nextHandler != null) {
                this.nextHandler.HandleMessage(daughter);
            } else { /* 为空 */
                System.out.println("拒绝");
            }
        }
    }

    /**
     * 设置下一个责任人
     */
    public void setNext(Handler handler) {
        this.nextHandler = handler;
    }

    /**
     * 处理结果
     */
    protected abstract void response(Daughter daughter);

}
